<?
/**
 *Party Module层，封装了与数据有关的操作
 */
class MParty
{
	private $_db;
	public function __construct()
	{
		$this->_db = Db::getInstance();
	}
	
	/**
	 * 添加聚会
	 * @param  $patry array 聚会信息数组
	 * @return boolean
	 */
	public function add_party($party)
	{
		//拼接生成聚会SQL
		$sql = "insert into gather (gatherId, userTel, name, theme, content, createDate, startTime,noteDate,place,number,allAttend, state)";
		$sql.= " values (null, '{$party['userTel']}','{$party['name']}', '{$party['theme']}', '{$party['content']}',now(), '{$party['startTime']}','{$party['noteDate']}','{$party['place']}','{$party['number']}','{$party['allAttend']}','0')";
		try
		{
			$this->_db->query($sql);
			return $this->_db->insert_id();
		}
		catch(Exception $e)
		{
			return false;
		}
	}
	
	
	/**
	 * 添加好友列表
	 * @param $friends 好友数组
	 * @return boolean
	 */
	public function add_friends($friends)
	{
		//添加好友
		$friends = explode(";",$friends);
		if(count($friends)>0)
		{
			$name = array();
			$tels = array();
			foreach($friends as $friend)
			{
				$tmp = explode("#", $friend);
				$name[] = $tmp[1];
				$tel[]	= $tmp[0];	
			}
			$user = $_SESSION['userTel'];
			
			for($i=0; $i<count($name); $i++)
			{
				if(!$this->is_friend_exists($name[$i], $tel[$i], $user))
				{
					if(!$this->add_friend($name[$i], $tel[$i], $user))
					{
						return false;
					}
				}
			}
		}
		return true;
	}
	
	/**
	 *判断好友是否存在
	 *@param  $name 好友姓名 $tel 好友手机号, $userId用户tel
	 *@return boolean
	 */
	public function is_friend_exists($name, $tel, $userTel)
	{
		$sql = "select * from friend where userTel='{$userTel}' and friendName='{$name}' and friendTel='{$tel}'";
		return $this->_db->num_rows($sql)>0 ? true : false;
	}
	
	/**
	 *添加一个好友
	 *@param  $name 好友姓名 $tel 好友手机号, $userId用户id
	 *@return 执行成功返回聚会Id
	 */
	public function add_friend($name, $tel, $userTel)
	{
		$sql = "insert into friend(Id, userTel, friendName, friendTel) ";
		$sql.= "values (null, '{$userTel}', '{$name}', '{$tel}')";
		try
		{
			$this->_db->query($sql);
			return true;
		}
		catch(Exception $e)
		{
			return false;
		}
	}
	
	
	/**
	 *添加参与者可选日期
	 *@param $launch_date 日期字符窜 $party_id 聚会Id
	 *@return 
	 */
	public function add_alternative_date($launch_date,$party_id)
	{
		$launch_date = explode(";" , $launch_date);
		if(count($launch_date)>0)
		{
			$user = $_SESSION['userTel'];
			foreach($launch_date as $date)
			{
				if($date!="")
				{
					$sql = "insert into gather_date (Id, gatherId, holdDate)";
					$sql.="values (null, '{$party_id}','{$date}')";
					try
					{
						$this->_db->query($sql);
					}
					catch (Exception $e)
					{
						return false;
					}					
				}
			}
		}
		return true;
	}
	
	
	/**
	 *添加所有被通知的人到发送队列
	 *即把被邀请者的信息添加到 gather_user表
	 *gather_user表中 state=0 短信还未发送，state=5 短信发送成功  state=10 用户有回复	state=15用户回复不想去
	 *
	 *@param $friends 好友字符串
	 *		 $party_id 聚会id
	 *@return boolean
	 */
	public function add_friends_to_sms_queue($friends,$party_id)
	{
		$friends = explode(";",$friends);
		if(count($friends)>0)
		{
			foreach($friends as $friend)
			{
				$tmp = explode("#", $friend);
				$name[] = $tmp[1];
				$tel[]	= $tmp[0];
				
				$sql = "insert into gather_user (Id, gatherId, attendTel, attendName, state) ";
				$sql.= "values (null, '{$party_id}', '{$tmp[0]}', '{$tmp[1]}','0')";
				try
				{
					$this->_db->query($sql);
				}
				catch(Exception $e)
				{
					return false;
				}
			}
			return true;
		}
		return true;
	}
	
	public function send_sms_to_friends($partyId)
	{
		
	}
	
	/**
	 *获取好友列表的字符串，供前台好友autocompelete使用
	 *格式 "XX 398039612","Jane 2244093694","ST 527936195","马逸清 447208649","庚庚 350294608","老板 312570154"
	 *@param null
	 *@return $friend_str
	 */
	public function get_friend_str()
	{
		$user = $_SESSION['userTel'];
		$sql = "select * from friend where userTel='{$user}'";
		$rst = $this->_db->fetch_all($sql);
		$tmp=array();
		if(count($rst)>0)
		{
			foreach($rst as $friend)
			{
				$str = "\"{$friend['friendName']} {$friend['friendTel']}\"";
				$tmp[] = $str;
			}
		}
		$friend_str = implode(",", $tmp);
		return $friend_str;
	}
	
	public function contactPeople($userTel)
	{
		try
		{
			$sql = "select `friendName` as `name`,`friendTel` as `tel` from `friend` where `userTel`={$userTel}";
			$result = $this->_db->fetch_all($sql);
			return $result;
		}
		catch(Exception $ex)
		{
			
		}
	}
}
?>